﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess.DataObject;
using System.Data;
using System.Data.SqlClient;

namespace DataAccess.BusinessObject
{
    public class BCoupon:Common.CDBase
    {
        public bool Add(OCoupon _OCoupon)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name",SqlDbType.NVarChar)
                                      , new SqlParameter("@Content",SqlDbType.NVarChar)
                                      , new SqlParameter("@StartDate",SqlDbType.DateTime)
                                      , new SqlParameter("@EndDate",SqlDbType.DateTime)  
                                      , new SqlParameter("@DiscountPercent",SqlDbType.Float)
                                      , new SqlParameter("@Status",SqlDbType.VarChar)
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OCoupon.Name;
            sqlParam[1].Value = _OCoupon.Content;
            sqlParam[2].Value = _OCoupon.StartDate;
            sqlParam[3].Value = _OCoupon.EndDate;
            sqlParam[4].Value = _OCoupon.DiscountPercent;
            sqlParam[5].Value = _OCoupon.Status;
            sqlParam[6].Value = _OCoupon.FeaturedImage;
            if (RunProcedure("sp_tblCoupon_add", sqlParam))
            {
                return true;
            }
            return false;
        }
        public IList<OCoupon> Get(int CouponID)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CouponID", SqlDbType.Int) };
            sqlParam[0].Value = CouponID;
            return Common.Common.ConvertTo<OCoupon>(RunProcedureGet("sp_tblCoupon_get", sqlParam));
        }
        public IList<OCoupon> Get(string Name, string Status, string Order, string OrderBy, int PageIndex, int PageSize)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@Name", SqlDbType.NVarChar),
                                        new SqlParameter("@Status",SqlDbType.VarChar),
                                        new SqlParameter("@Order",SqlDbType.VarChar),
                                        new SqlParameter("@OrderBy",SqlDbType.VarChar),
                                        new SqlParameter("@PageIndex",SqlDbType.Int),
                                        new SqlParameter("@PageSize",SqlDbType.Int)};
            sqlParam[0].Value = Name;           
            sqlParam[1].Value = Status;
            sqlParam[2].Value = Order;
            sqlParam[3].Value = OrderBy;
            sqlParam[4].Value = PageIndex;
            sqlParam[5].Value = PageSize;
            return Common.Common.ConvertTo<OCoupon>(RunProcedureGet("sp_tblCoupon_get", sqlParam));
        }
        
        public bool Update(OCoupon _OCoupon)
        {
            SqlParameter[] sqlParam = { new SqlParameter("@CouponID",SqlDbType.Int)
                                      , new SqlParameter("@Name",SqlDbType.NVarChar)
                                      , new SqlParameter("@Content",SqlDbType.NVarChar)
                                      , new SqlParameter("@StartDate",SqlDbType.DateTime)
                                      , new SqlParameter("@EndDate",SqlDbType.DateTime)
                                      , new SqlParameter("@DiscountPercent",SqlDbType.Float)
                                      , new SqlParameter("@Status",SqlDbType.VarChar)                                      
                                      , new SqlParameter("@FeaturedImage",SqlDbType.NVarChar)};
            sqlParam[0].Value = _OCoupon.CouponID;
            sqlParam[1].Value = _OCoupon.Name;
            sqlParam[2].Value = _OCoupon.Content;
            sqlParam[3].Value = _OCoupon.StartDate;
            sqlParam[4].Value = _OCoupon.EndDate;
            sqlParam[5].Value = _OCoupon.DiscountPercent;
            sqlParam[6].Value = _OCoupon.Status;
            sqlParam[7].Value = _OCoupon.FeaturedImage;            
            if (RunProcedure("sp_tblCoupon_update", sqlParam))
            {                
                return true;
            }
            return false;
        }
        public int GetIDLastRecord()
        {
            DataTable dtb = RunProcedureGet("sp_tblCoupon_getidlastrecord");
            if (dtb.Rows.Count > 0)
            {
                return int.Parse(dtb.Rows[0]["CouponID"].ToString());
            }
            return 0;
        }
        public bool Delete(int CouponID)
        {                       
            SqlParameter[] sqlParam = { new SqlParameter("@CouponID", SqlDbType.Int) };
            sqlParam[0].Value = CouponID;
            return RunProcedure("sp_tblCoupon_delete", sqlParam);
        }
    }
}
